package com.ds.infrastructure.hyperspace.console.model.request.deploy.deploydata;

import com.ds.infrastructure.hyperspace.console.dto.deploydata.DeployDataDTO;
import com.ds.infrastructure.hyperspace.console.dto.deploydata.HostGroupDTO;
import com.ds.infrastructure.hyperspace.console.dto.deploydata.VipDTO;
import com.ds.infrastructure.hyperspace.console.dto.deploydata.VipMapDTO;
import com.ds.infrastructure.hyperspace.console.utils.ValidationUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-10-21 10:41
 **/
@Data
@NoArgsConstructor
public class DeployDataForm {
    private PackageInfoForm packageInfo;
    private List<HostGroupForm> hostGroups;
    private List<VipForm> vips;
    private List<VipMapForm> vipMaps;

    public static DeployDataDTO toDTO(DeployDataForm form) {
        DeployDataDTO deployDataDTO = new DeployDataDTO();
        addPackageInfoDTO(form, deployDataDTO);
        addHostGroupsDTO(form, deployDataDTO);
        addVipsDTO(form, deployDataDTO);
        addVipMapsDTO(form, deployDataDTO);
        return deployDataDTO;
    }

    // region toDTO
    private static void addVipMapsDTO(DeployDataForm form, DeployDataDTO deployDataDTO) {
        if (form.getVipMaps() == null || form.getVipMaps().isEmpty()) {
            return;
        }
        ValidationUtil.validateEntityList(form.getVipMaps(), "vipMaps");
        List<VipMapDTO> vipMapDTOList = new ArrayList<>();
        form.getVipMaps().forEach(item -> vipMapDTOList.add(VipMapForm.toDTO(item)));
        deployDataDTO.setVipMaps(vipMapDTOList);
    }

    private static void addVipsDTO(DeployDataForm form, DeployDataDTO deployDataDTO) {
        if (form.getVips() == null || form.getVips().isEmpty()) {
            return;
        }
        ValidationUtil.validateEntityList(form.getVips(), "vips");
        List<VipDTO> vipDTOList = new ArrayList<>();
        form.getVips().forEach(item -> vipDTOList.add(VipForm.toDTO(item)));
        deployDataDTO.setVips(vipDTOList);
    }

    private static void addHostGroupsDTO(DeployDataForm form, DeployDataDTO deployDataDTO) {
        if (form.getHostGroups() == null || form.getHostGroups().isEmpty()) {
            return;
        }
        ValidationUtil.validateEntityList(form.getHostGroups(), "hostGroups");
        List<HostGroupDTO> hostGroupDTOList = new ArrayList<>();
        form.getHostGroups().forEach(item -> hostGroupDTOList.add(HostGroupForm.toDTO(item)));
        deployDataDTO.setHostGroups(hostGroupDTOList);
    }

    private static void addPackageInfoDTO(DeployDataForm form, DeployDataDTO deployDataDTO) {
        if (form.getPackageInfo() == null) {
            return;
        }
        ValidationUtil.validateEntity(form.getPackageInfo());
        deployDataDTO.setPackageInfo(PackageInfoForm.toDTO(form.getPackageInfo()));
    }

    // endregion
    public static DeployDataForm toVO(DeployDataDTO dto) {
        DeployDataForm form = new DeployDataForm();
        addPackageInfoForm(form, dto);
        addHostGroupsForm(form, dto);
        addVipsVO(form, dto);
        addVipMapsVO(form, dto);
        return form;
    }

    private static void addVipMapsVO(DeployDataForm form, DeployDataDTO dto) {
        if (dto.getVipMaps() == null || dto.getVipMaps().isEmpty()) {
            return;
        }
        List<VipMapForm> vipMapFormList = new ArrayList<>();
        dto.getVipMaps().forEach(item -> vipMapFormList.add(VipMapForm.toVO(item)));
        form.setVipMaps(vipMapFormList);
    }

    private static void addVipsVO(DeployDataForm form, DeployDataDTO dto) {
        if (dto.getVips() == null || dto.getVips().isEmpty()) {
            return;
        }
        List<VipForm> vipFormList = new ArrayList<>();
        dto.getVips().forEach(item -> vipFormList.add(VipForm.toVO(item)));
        form.setVips(vipFormList);
    }

    private static void addHostGroupsForm(DeployDataForm form, DeployDataDTO dto) {
        if (dto.getHostGroups() == null || dto.getHostGroups().isEmpty()) {
            return;
        }
        List<HostGroupForm> hostGroupFormList = new ArrayList<>();
        dto.getHostGroups().forEach(item -> hostGroupFormList.add(HostGroupForm.toVO(item)));
        form.setHostGroups(hostGroupFormList);
    }

    private static void addPackageInfoForm(DeployDataForm form, DeployDataDTO dto) {
        if (dto.getPackageInfo() == null) {
            return;
        }
        form.setPackageInfo(PackageInfoForm.toVO(dto.getPackageInfo()));
    }
}
